/**
 * [OElove] (C)2010-2099 OELOVE.COM Inc.biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2017/01/16 by CL $
*/
$(function(){
	//加载下一页评论
	$(document).on("click", "[f='but_get_commpage']", function(){
		$_page = parseInt($(this).attr("data-page"));
		if ($_page > 1) {
			$("[f='but_get_commpage']").find("label").show();
		}
		_getCommPageList($_page);
	});

	//保存评论
	$(document).on("click", "[f='but_save_addcomm']", function(){
		$_content = $("#comm_content").val();
		$_storyid = $("#storyid").val();
		if ($_content.length == 0) {
			return false;
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH+"index.php?c=story",
			cache: false,
			data: {a:"savecomm", storyid:$_storyid, content:$_content, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == '1') {
					$("#comm_content").val("");
					//关闭评论窗
					$("#story_addcomm_data").yanVhSlideCenter(100, function(){
						$("#story_addcomm_data").html("");
						$("#story_addcomm_box").remove();
						$("html").removeClass("dialog_lock_hide"); //滚动条
						ToastShow("发表成功");
						_getCommPageList(1); //重新加载评论
					});
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("保存失败");
					}
				}
			},
			error: function() {
			}
		});

	});

	//弹出评论页面 JSON
	$(document).on("click", "[f='but_open_addcomm']", function(){
		$ajax_url = _ROOT_PATH+"index.php?c=story&a=addcomm";
		oeShadeBox("story_addcomm", "ajax", $ajax_url, 600, 310);
	});

});

//ajax加载评论分页
function _getCommPageList($page) {
	if (typeof($page) == "undefined") {
		$page = 1;
	}
	$obj_box = $("#comm_json_data");
	$.ajax({
		type: "GET",
		url:  _ROOT_PATH+"index.php?c=story&a=comm&datatype=json&storyid="+$("#storyid").val(),
		cache: false,
		data: {page:$page, r:get_rndnum(8)},
		dataType: "json",
		success: function($data) {
			$json = eval($data);
			$prepage = $json.prepage; //上一页
			$nextpage = $json.nextpage; //下一页
			$result = $json.result;
			if ($result.length > 0) {
				if ($page == "1") {
					$obj_box.html($result);
				}
				else {
					$obj_box.append($result);
				}
				if ($nextpage > 0) { //还有下一页
					$("[f='but_get_commpage']").attr("data-page", $nextpage);
					$("[f='but_get_commpage']").show();
					$("[f='but_get_commpage']").find("label").hide();
				}
				else { 
					$("[f='but_get_commpage']").hide();
				}
			}
		},
		error: function() {
		
		}
	});
}
